﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Datum;
using Aribaccio.Security;
using BDPAutodetect.Models;

namespace BDPAutodetect.DataService
{
    /// <summary>
    /// 基础数据业务处理类
    /// </summary>
    public class DataServiceBasic : DataServiceBase, IDataServiceBasic
    {
        /// <summary>
        /// 加密解密辅助类
        /// </summary>
        protected ISecurity Security { get; set; }
        /// <summary>
        /// 检测任务线程
        /// </summary>
        protected PeriodicTaskManual MonitorTask { get; set; }
        /// <summary>
        /// 
        /// </summary>
        protected ConcurrentQueue<string> StoreDataSqlQueue { get; set; }

        /// <summary>
        /// 带参构造函数[注入]
        /// </summary>
        /// <param name="argCoreDBContextFactory">数据库连接操作上下文-工厂接口实例对象</param>
        /// <param name="argSecurity">加密解密辅助类</param>
        public DataServiceBasic(ICoreDBContextFactory argCoreDBContextFactory, ISecurity argSecurity)
            : base(argCoreDBContextFactory)
        {
            this.Security = argSecurity;
            this.StoreDataSqlQueue = new ConcurrentQueue<string>();
            this.MonitorTask = new PeriodicTaskManual($"数据库任务", 100, MonitorTaskAction, false, false);
        }

        /// <summary>
        /// 执行线程任务
        /// </summary>
        protected void MonitorTaskAction()
        {
            if (this.StoreDataSqlQueue.Count <= 0) return;

            while (this.StoreDataSqlQueue.TryDequeue(out string? storeDataSql))
            {
                if (string.IsNullOrWhiteSpace(storeDataSql)) continue;
                this.ChangesExecuteSQL(storeDataSql);
            }
        }

        /// <summary>
        /// 插入队列存储数据
        /// </summary>
        public void EnqueueStoreData(string argDataSql)
        {
            this.StoreDataSqlQueue.Enqueue(argDataSql);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="argLoginDto">用户传输模型对象</param>
        /// <returns></returns>
        public ResultModel<T> UserLogin<T>(MLoginDto argLoginDto) where T : TBasicUser
        {
            ResultModel<T> result = new ResultModel<T>(ResultStatus.Failure);
            if (argLoginDto == null)
            {
                result.ResultMsg = "传入参数异常";
                return result;
            }
            ResultModel<T> resultUser = this.SelectModelItem<T>(v => v.Name == argLoginDto.UserName);
            if (resultUser == null || !resultUser.ResultState || resultUser.Data == null)
            {
                result.ResultMsg = "用户不存在";
                return result;
            }
            if (resultUser.Data.Freezed != 0)
            {
                result.ResultMsg = "用户被冻结";
                return result;
            }
            string strSecurityPassword = this.Encrypt(argLoginDto.UserPassword);
            if (resultUser.Data.Password != strSecurityPassword)
            {
                result.ResultMsg = "密码错误";
                return result;
            }
            result.ResultCode = ResultStatus.Succeed;
            result.Data = resultUser.Data;
            return result;
        }

        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="argEncryptString">待加密的明文</param>
        /// <returns>加密密文字符串</returns>
        public string Encrypt(string argEncryptString)
        {
          return  this.Security.Encrypt(argEncryptString);
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="argDecryptString">待解密的密文</param>
        /// <returns>解密明文字符串</returns>
        public string Decrypt(string argDecryptString)
        {
            return this.Security.Decrypt(argDecryptString);
        }
    }
}
